package com.hsgene.user.service.impl;

import com.hsgene.common.util.result.ApiResult;
import com.hsgene.user.constants.UserIntType;
import com.hsgene.user.converter.UserConverter;
import com.hsgene.user.domain.GeneticTestingOperationAccount;
import com.hsgene.user.domain.GeneticTestingUser;
import com.hsgene.user.dto.user.GeneticTestingUserDto;
import com.hsgene.user.exception.UserErrorCode;
import com.hsgene.user.persistence.OperationAccountRepository;
import com.hsgene.user.persistence.GeneticTestingUserRepository;
import com.hsgene.user.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 用户ServiceImpl
 *
 * @author wangbing
 * @version 1.0, 2018/10/17
 */
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private GeneticTestingUserRepository geneticTestingUserRepository;

    @Resource
    private OperationAccountRepository operationAccountRepository;

    @Override
    public ApiResult<GeneticTestingUserDto> findByUserId(String userId, UserIntType userType) {
        GeneticTestingUserDto result = null;
        if (UserIntType.MANAGER.equals(userType)) {
            GeneticTestingOperationAccount operationAccount = operationAccountRepository.findById(userId);
            if (operationAccount != null) {
                result = UserConverter.toGeneticTestingUserDto(operationAccount);
            }
        } else {
            GeneticTestingUser user = geneticTestingUserRepository.findById(userId);
            if (user!= null) {
                result = UserConverter.toGeneticTestingUserDto(user);
            }
        }
        if (result == null) {
            return ApiResult.fail(UserErrorCode.USER_NOT_EXISTED);
        }
        return ApiResult.succ(result);
    }
}
